#pragma once

#include <queue>
#include <vector>

namespace solution_295 {

class MedianFinder
{
public:
    void addNum(int num) {
        if (max_heap_.empty() || num <= max_heap_.top()) {
            max_heap_.push(num);
            if (max_heap_.size() > min_heap_.size() + 1) {
                min_heap_.push(max_heap_.top());
                max_heap_.pop();
            }
        } else {
            min_heap_.push(num);
            if (max_heap_.size() < min_heap_.size()) {
                max_heap_.push(min_heap_.top());
                min_heap_.pop();
            }
        }
    }

    double findMedian() {
        if (max_heap_.size() == min_heap_.size())
            return (max_heap_.top() + min_heap_.top()) / 2.0;
        else
            return max_heap_.top();
    }

private:
    /* smaller partition */
    std::priority_queue<int> max_heap_;

    /* bigger partition */
    std::priority_queue<int, std::vector<int>, std::greater<int>> min_heap_;
};

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj->addNum(num);
 * double param_2 = obj->findMedian();
 */

}  /* namespace solution_295 */
